package edu.scau.cxq.gp.controller;

import com.github.pagehelper.PageInfo;
import edu.scau.cxq.gp.common.annotation.ResponseResult;
import edu.scau.cxq.gp.entity.SystemNotice;
import edu.scau.cxq.gp.entity.vo.NoticeVO;
import edu.scau.cxq.gp.entity.vo.UserVO;
import edu.scau.cxq.gp.service.SystemNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@RestController
@ResponseResult
@RequestMapping("/notice")
public class SystemNoticeController {

    @Resource
    private SystemNoticeService systemNoticeService;

    /**
     * 分页查询公告
     * @return
     */
    @GetMapping("/selectByPage")
    public PageInfo<SystemNotice> selectByPage (Integer currentPage, Integer size, NoticeVO noticeVO) {
        System.out.println(noticeVO+":"+currentPage+":"+size);
        PageInfo<SystemNotice> systemNoticePageInfo = systemNoticeService.getNoticeList(currentPage, size,noticeVO);
        return systemNoticePageInfo;
    }

    /**
     * 获取系统公告列表
     */
    @GetMapping("/list")
    public List<SystemNotice> getSystemNoticeList() {
        return systemNoticeService.getSystemNoticeList();
    }

    /**
     * 根据id获取系统公告
     * @param id 系统公告id
     */
    @GetMapping("/status/{id}")
    public SystemNotice getSystemNoticeById(@PathVariable("id") int id) {
        return systemNoticeService.getSystemNoticeById(id);
    }

    /**
     * 创建系统公告
     */
    @PostMapping("/addNotice")
    public boolean createSystemNotice(@RequestBody NoticeVO noticeVO) {
        SystemNotice systemNotice = new SystemNotice()
                .setCreatedAt(new Date())
                .setUpdatedAt(new Date())
                .setStatus(0)
                .setTitle(noticeVO.getTitle())
                .setContent(noticeVO.getContent());
        return systemNoticeService.createSystemNotice(systemNotice);
    }

    /**
     * 更新系统公告
     * @param systemNotice 系统公告
     */
    @PostMapping("/updateStatus")
    public boolean updateSystemNotice(@RequestBody SystemNotice systemNotice) {
        int id = systemNotice.getId();
        SystemNotice systemNoticeById = systemNoticeService.getNotice(id);
        if (systemNoticeById.getStatus()==1){
            systemNoticeById.setStatus(0);
            systemNoticeService.updateSystemNotice(systemNoticeById);
        }else {
            List<SystemNotice> systemNoticeList = systemNoticeService.getSystemNoticeList();
            for (SystemNotice notice : systemNoticeList) {
                if (notice.getId() != id && notice.getStatus() == 1) {
                    return false;
                }
            }
            systemNoticeById.setStatus(1);
            systemNoticeService.updateSystemNotice(systemNoticeById);
        }
        return true;
    }

    /**
     * 删除系统公告
     * @param id 系统公告id
     */
    @DeleteMapping("/{id}")
    public boolean deleteSystemNotice(@PathVariable("id") int id) {
        return systemNoticeService.deleteSystemNotice(id);
    }
}
